<template>
  <div style="padding:20px">
    <h1>{{id ? '编辑':'新建'}}装备</h1>
    <el-alert
      v-if = "id?true:false"
      title="如果要更改装备归属，请删除该装备后重新添加！"
      type="warning"
      center
      show-icon>
    </el-alert>
    <el-form label-width = "120px" :rules = "rules" ref = "ruleFormRef" :model = "model">
      <el-form-item label = "【装备名称】" prop = "name">
        <el-input v-model="model.name" placeholder = "请输入装备名称" style="width:300px"></el-input>
      </el-form-item>
      <el-form-item label = "【装备图标】" prop = "icon">
        <el-upload
          class="avatar-uploader"
          :action="$upload"
          :show-file-list="false"
          accept = "image/jpg,image/jpeg,image/png"
          :before-upload="$handleBeforeUpload.handleBeforeUpload"
          :on-success="afterUpload">
          <img v-if="model.icon" :src="$upload+'/'+model.icon" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label = "【装备归属】" prop = "f_id">
        <el-select v-model = "model.f_id" placeholder = "请选择装备归属" :disabled = "id?true:false">
          <el-option v-for = "(item,i) in $store.state.items" :value = "item._id" :label = 'item.name' :key = "i"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label = "【装备价格】" prop = "price">
        <el-input v-model.number="model.price" placeholder = "请输入装备价格" type = "number" style="width:220px"></el-input>
      </el-form-item>
      <el-form-item label = "【装备描述】" prop = "descibe">
        <el-input v-model="model.descibe" placeholder = "请输入装备描述"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type = 'primary' @click="save">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  props: ['id'],
  data () {
    return {
      model: {
        descibe: '',
        icon: '',
        name: '',
        price: '',
        f_id: ''
      },
      rules: {
        f_id: {
          required: true,
          message: '请选择装备归属',
          trigger: 'blur'
        },
        name: {
          required: true,
          message: '请填写装备名称',
          trigger: 'blur'
        },
        icon: {
          required: true,
          message: '请选择装备图片',
          trigger: 'blur'
        },
        descibe: {
          required: true,
          message: '请填写装备描述',
          trigger: 'blur'
        },
        price: {
          required: true,
          message: '请填写装备价格',
          trigger: 'blur'
        }
      }
    }
  },
  methods: {
    async save () {
      this.$refs.ruleFormRef.validate(async (valid) => {
        if (!valid) {
          return this.$message.warning('请正确填写所有相关信息')
        }
        const res = this.id ? await this.$http.put('rest/Item', { _id: this.model.f_id, index: this.id, children: this.model }) : await this.$http.post('rest/Item/add', { _id: this.model.f_id, model: this.model })
        if (res.status !== 200) {
          return this.$message.error(this.id ? '修改装备失败' : '添加装备失败')
        }
        this.$message.success(this.id ? '修改装备成功' : '添加装备成功')
        this.$store.commit('UpdataItem', res.data)
        window.sessionStorage.setItem('activePath', '/item/list')
        window.sessionStorage.setItem('ActivityName', this.model.f_id)
        this.$router.push({ path: '/item/list' })
      })
    },
    async fetch () {
      await this.$http.get(`rest/Item/${this.id}`).then(res => {
        if (res.status !== 200) {
          window.sessionStorage.setItem('activePath', '/item/list')
          this.$router.push({ path: '/item/list' })
          return this.$message.error('获取装备失败')
        }
        this.model = res.data
      })
    },
    afterUpload (res) {
      this.model.icon = res.url
    }
  },
  created () {
    this.id && this.fetch()
  },
  watch: {
    '$route.path': function (newVal) {
      if (newVal === '/item/create') {
        this.$data.model = { ...this.$options.data().model }
      }
    }
  }
}
</script>

<style scoped>
  .el-alert{
    margin-bottom: 10px;
  }
</style>
